package com.biz.service.home;

import com.biz.model.Hmodel.TMenu;
import com.biz.model.Pmodel.Pmenu;
import com.biz.model.Pmodel.User;
import com.biz.model.Pmodel.basic.Pagent;
import com.biz.model.Pmodel.basic.Parea;
import com.biz.model.Pmodel.basic.Pbrand;
import com.biz.service.base.BaseServiceImpl;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by yzljj on 2016/6/23.
 */
@Service("homeService")
public class HomeServiceImpl extends BaseServiceImpl<Object> implements HomeServiceI {

    private  SimpleDateFormat  shortSdf;
	@Autowired
	private BaseDaoI<TMenu> menuDao;
	
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	@Autowired
	private BaseDaoI baseDao;
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Pmenu> showLeftMenu() throws Exception {
		List<Pmenu> pmenuList=null;
		pmenuList=(List<Pmenu>)dao.findForList("homeDao.findAllMenu", null);
		return pmenuList;
	}

    @Override
    public List<Pmenu> showLeftMenuByRole(String deep) throws Exception {
        List<Pmenu> pmenuList=null;
        Map<String, Object> paramsMap=new HashMap<String, Object>();
        paramsMap.put("deep",deep);
        pmenuList=(List<Pmenu>)dao.findForList("homeDao.showLeftMenuByRole", paramsMap);
        return pmenuList;
    }

	@Override
	public List<Parea> getList(String deep) throws Exception {
		return (List<Parea>)dao.findForList("homeDao.getList", deep);
	}

	@Override
	public List<Parea> getList(String lev, String id) throws Exception {
		Map<String, Object> paramsMap=new HashMap<String, Object>();
		paramsMap.put("deep",lev);
		paramsMap.put("id",id);
		List<Parea> pareaList=(List<Parea>)dao.findForList("homeDao.getListParm", paramsMap);
		return pareaList;
	}

	@Override
	public List<Parea> getListByPid(String lev, String pid) throws Exception {
		Map<String, Object> paramsMap=new HashMap<String, Object>();
		paramsMap.put("deep",lev);
		paramsMap.put("pid",pid);
		List<Parea> pareaList=(List<Parea>)dao.findForList("homeDao.getListParmByPid", paramsMap);
		return pareaList;
	}





    @Override
    public Parea getAreaById(String cityId) throws Exception {
        return (Parea) dao.findForObject("homeDao.getAreaById",cityId);
    }

    private List<Map<String,Object>> getFullDayData(List<Map<String, Object>> list, String beginTime, String endTime) throws ParseException {
        List<Map<String,Object>> listRes=new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date dateBegin=sdf.parse(beginTime);
        Date dateEnd=sdf.parse(endTime);
        Date dateNow=dateBegin;
        for(int i=0;dateNow.getTime()<=dateEnd.getTime();i++)
        {
            Map<String,Object> map=new HashMap<>();
            String dateNowString=sdf.format(dateNow);
            map.put("date",dateNow.getTime());
            Calendar c = Calendar.getInstance();
            c.setTime(dateNow);
            c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
            dateNow = c.getTime();



            for(int j=0;j<list.size();j++)
            {
                if(dateNowString.equals(list.get(j).get("date")))
                {
                    map.put("count",list.get(j).get("count"));
                    break;
                }
                else
                {
                    map.put("count",BigDecimal.valueOf(0.0));
                }
            }
            if(list.size()==0)
            { map.put("count",BigDecimal.valueOf(0.0));}
            listRes.add(map);
        }
      /*  Map<String,Object> map=new HashMap<>();
        map.put("date",dateNow.getTime());
        map.put("count",BigDecimal.valueOf(0.0));
        listRes.add(map);*/
        return listRes;
    }


    /**
     * 获得本周的第一天，周一
     *
     * @return
     */
    public   Date getCurrentWeekDayStartTime() {
        shortSdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            c.setTime(shortSdf.parse(shortSdf.format(c.getTime())));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /**
     * 获得本周的最后一天，周日
     *
     * @return
     */
    public Date getCurrentWeekDayEndTime() {
        shortSdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            c.add(Calendar.DATE, 8 - weekday);
            c.setTime(shortSdf.parse(shortSdf.format(c.getTime())));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    /**
     * 获得本月的开始时间，即2012-01-01 00:00:00
     *
     * @return
     */
    public   Date getCurrentMonthStartTime() {
        shortSdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前月的结束时间，即2012-01-31 23:59:59
     *
     * @return
     */
    public   Date getCurrentMonthEndTime() {
        shortSdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的开始时间，即2012-01-1 00:00:00
     *
     * @return
     */
    public   Date getCurrentQuarterStartTime() {
        shortSdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的结束时间，即2012-03-31 23:59:59
     *
     * @return
     */
    public   Date getCurrentQuarterEndTime() {
        shortSdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
}
